import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home/Home.vue";
import Main from "../views/Main.vue";
import Login from "../views/Login/Login.vue";
import OrderList from "../views/Order/OrderList.vue";
import OrderCollect from "../views/Order/OrderCollect.vue";
import ProductList from "../views/Product/ProductList.vue";
import ProductClassify from "@/views/Product/ProductClassify.vue";
import PageOne from "../views/Advertising/PageOne.vue";
import PageTwo from "../views/Advertising/PageTwo.vue";
//引入获取token方法
import { getToken } from "@/utils/storage";
//导入提示
import { Message } from "element-ui";

Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    name: "login",
    component: Login,
  },
  {
    path: "/",
    component: Main,
    redirect: "/home",
    children: [
      {
        path: "home",
        name: "home",
        component: Home,
      },
      {
        path: "productlist",
        name: "productlist",
        component: ProductList,
      },
      {
        path: "productclassifg",
        name: "productclassifg",
        component: ProductClassify,
      },
      {
        path: "orderlist",
        name: "orderlist",
        component: OrderList,
      },
      {
        path: "ordercollect",
        name: "ordercollect",
        component: OrderCollect,
      },
      {
        path: "page1",
        name: "page1",
        component: PageOne,
      },
      {
        path: "page2",
        name: "page2",
        component: PageTwo,
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  // 在这里添加你的全局前置守卫逻辑

  // 检查用户是否已登录
  const token = getToken("token");
  if (!token && to.name !== "login") {
    // 路由需要登录权限，但用户未登录，重定向到登录页面
    next({ name: "login" });
  } else if (token && to.name === "login") {
    Message({
      message: "您已登陆，请先退出",
      type: "warning",
    });
    //用户已经登陆直接显示home页面
    next({ name: "home" });
  } else {
    next();
  }
});
export default router;
